/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.TopoDao;
import com.sinodata.bsm.center.user.UserUtil;
import com.sinodata.bsm.common.vo.TopuConf;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author sunxiangxue
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-10 下午4:48:06          sunxiangxue        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Repository
public class TopoDaoImpl extends BaseDao implements TopoDao {

    Logger logger = Logger.getLogger(TopoDaoImpl.class);

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.dao.TopoDao#find()
     */
    @Override
    public List<TopuConf> findByExample(TopuConf topuConf) {
        //Criteria a = getSession().createCriteria(TopuConf.class).add(Example.);
        return getHibernateTemplate().findByExample(topuConf);
    }

    @Override
    public void deleteTopuPlayByUserId(String userId) {
        String sql = "delete from topu_play_order where user_id='" + userId + "'";
        this.getSession().createSQLQuery(sql).executeUpdate();
    }

    @Override
    public void deleteTopoPlayOrderByTopoId(Long id) {
        String sql = "delete from topu_play_order where topu_conf_id=" + id;
        this.getSession().createSQLQuery(sql).executeUpdate();
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.dao.TopoDao#findShowTopos()
     */
    @Override
    public List<TopuConf> findShowTopos() {
        String userId = UserUtil.getInstance().getCurrentUserIdByFlex();
        //String userId = "sunxx1984";
        logger.debug("coming into findShowTopos ,current UserId = " + userId);
        StringBuffer sql = new StringBuffer("select tc.id, tc.name, tc.res_id, tc.xml, tc.catalog_id ");
        sql.append(" from topu_play_order tpo, topu_conf tc ");
        sql.append(" where tc.id = tpo.topu_conf_id ");
        sql.append(" and tpo.user_id = '" + userId + "'");
        sql.append(" order by tpo.orderby ");
        logger.debug("findShowTopos,select SQL =" + sql.toString());
        List<TopuConf> list = this.getSession().createSQLQuery(sql.toString()).addEntity(TopuConf.class).list();
        return list;
    }

    public List<TopuConf> findShowTopos(String topoId) {
        if (topoId == null || "".equals(topoId))
            return new ArrayList<TopuConf>();
        StringBuffer sql = new StringBuffer("select tc.id, tc.name, tc.res_id, tc.xml, tc.catalog_id ");
        sql.append(" from topu_conf tc ");
        sql.append(" where tc.id in ( " + topoId + ") ");
        List<TopuConf> list = this.getSession().createSQLQuery(sql.toString()).addEntity(TopuConf.class).list();
        return list;
    }
}
